package demo.service;

import java.io.IOException;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import io.moquette.interception.InterceptHandler;
import io.moquette.interception.messages.InterceptAcknowledgedMessage;
import io.moquette.interception.messages.InterceptConnectMessage;
import io.moquette.interception.messages.InterceptConnectionLostMessage;
import io.moquette.interception.messages.InterceptDisconnectMessage;
import io.moquette.interception.messages.InterceptPublishMessage;
import io.moquette.interception.messages.InterceptSubscribeMessage;
import io.moquette.interception.messages.InterceptUnsubscribeMessage;
import io.moquette.server.Server;
import io.moquette.server.config.MemoryConfig;
import io.moquette.spi.security.IAuthenticator;
import io.moquette.spi.security.IAuthorizator;

@Service
public class MqttServerStart implements IAuthenticator,IAuthorizator,InterceptHandler{
	protected Server server;
	private static final Logger LOG=LoggerFactory.getLogger(MqttServerStart.class);
	
	
	@PostConstruct
	public void init() throws IOException{
		server=new Server();
		List<InterceptHandler> handlers=new ArrayList<InterceptHandler>(1);
		handlers.add(this);
		Properties properties=new Properties();
		properties.load(MqttServerStart.class.getResourceAsStream("/config/moquette.properties"));
		server.startServer(new MemoryConfig(properties),handlers,null,this,this);
		
	}
	@PreDestroy
	public void destory(){
		server.stopServer();
	}
	@Override
	public void onConnect(InterceptConnectMessage msg) {
		LOG.info(msg.getClientID()+"已经连接");
	}
	@Override
	public void onDisconnect(InterceptDisconnectMessage msg) {
		LOG.info(msg.getClientID()+"断开连接");
	}
	@Override
	public void onConnectionLost(InterceptConnectionLostMessage msg) {
		LOG.info(msg.getClientID()+"连接丢失");
	}
	@Override
	public void onPublish(InterceptPublishMessage msg) {
		
		msg.getPayload().order(ByteOrder.LITTLE_ENDIAN);
		short a=msg.getPayload().getShort();
		System.out.println(a+"==================================linit "+msg.getPayload().limit()+"pos"+msg.getPayload().position());
		
		
		LOG.info(msg.getClientID()+"主题"+msg.getTopicName()+":消息："+new String(msg.getPayload().array()));
	}
	@Override
	public void onSubscribe(InterceptSubscribeMessage msg) {
		LOG.info(msg.getClientID()+"订阅了主题"+msg.getTopicFilter());
	}
	@Override
	public void onUnsubscribe(InterceptUnsubscribeMessage msg) {
		LOG.info(msg.getClientID()+"取消主题订阅"+msg.getTopicFilter());
	}
	@Override
	public void onMessageAcknowledged(InterceptAcknowledgedMessage msg) {
		
	}
	@Override
	public boolean canWrite(String topic, String user, String client) {
		return true;
	}
	@Override
	public boolean canRead(String topic, String user, String client) {
		return true;
	}
	@Override
	public boolean checkValid(String clientId, String username, byte[] password) {
		System.out.println(clientId+"进来 username"+clientId);
		return true;
	}
	public Server getServer() {
		return server;
	}
	public void setServer(Server server) {
		this.server = server;
	}
	
	
}
